<?php
class fragen_controller extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		if($this->session->userdata('logged_in'))
   		{
			$this->load->model('fragen_model');
			$this->load->model('fragenart_model');
		}
   		else
   		{
     		//If no session, redirect to login page
     		redirect('welcome', 'refresh');
   		}
	}

	public function index()
	{
		$data['fragen'] = $this->fragen_model->get_fragen();

		$data['_scripts'] = '';
	 	$data['_styles'] = '';
		$data['head'] = '';
		$data['schulname'] = 'Berufskolleg Uerdingen';
		$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
		$data['login'] = '';
		$data['content'] = $this->load->view('fragen/fragenindex_view', $data,TRUE);
		$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
		
		$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
	}
	
	public function create_view()
	{
		$data['fragenart'] = $this->fragenart_model->get_fragenart();
			
		$data['_scripts'] = '';
	 	$data['_styles'] = '';
		$data['head'] = '';
		$data['schulname'] = 'Berufskolleg Uerdingen';
		$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
		$data['login'] = '';
		$data['content'] = $this->load->view('fragen/fragencreate_view', $data,TRUE);
		$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
		
		$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
	}
	
	public function create()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Fragenart', 'Fragenart', 'required');														   
		$this->form_validation->set_rules('Frage', 'Frage', 'required');

		if ($this->form_validation->run() === FALSE)
		{
			$this->create_view();
		}
		else
		{
			$this->fragen_model->set_fragen();			
			$this->fragenlistview();
		}		
	}
	
	public function validate_view()
	{
		$fragenid = $this->input->post('fragenid');
		$data['fragen'] = $this->fragen_model->get_fragen_id($fragenid);
		$data['fragenart'] = $this->fragenart_model->get_fragenart();
		
		$data['_scripts'] = '';
	 	$data['_styles'] = '';
		$data['head'] = '';
		$data['schulname'] = 'Berufskolleg Uerdingen';
		$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
		$data['login'] = '';
		$data['content'] = $this->load->view('fragen/fragenvali_view', $data,TRUE);
		$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
		
		$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
	}
	
	public function validate()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Fragenart', 'Fragenart', 'required');														   
		$this->form_validation->set_rules('Frage', 'Frage', 'required');

		if ($this->form_validation->run() === FALSE)
		{	
			$this->validate_view();
		}
		else
		{
			$fragenid= $this->input->post('fragenid');
			$this->fragen_model->validate_fragen($fragenid);

			$this->fragenlistview();
		}	 
	}
	
	public function fragenlistview($page = 0)
	{	
		$limit = 5;
		$data['fragen'] = $this->fragen_model->get_all($limit, $page * $limit);
		$data['pages'] = ceil($this->fragen_model->get_count() / $limit);
		
		$data['_scripts'] = '';
	 	$data['_styles'] = '';
		$data['head'] = '';
		$data['schulname'] = 'Berufskolleg Uerdingen';
		$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
		$data['login'] = '';
		$data['content'] = $this->load->view('fragen/fragenlist_view',$data,TRUE);
		$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
		
		$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
	}
}